package com.lixh.roommanager.ui.main

import android.graphics.Color
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.ActionBar
import androidx.appcompat.app.AppCompatActivity
import com.github.salomonbrys.kotson.fromJson
import com.google.gson.Gson
import com.jzxiang.pickerview.TimePickerDialog
import com.jzxiang.pickerview.data.Type
import com.lixh.roommanager.R
import com.lixh.roommanager.ui.Constant
import com.lixh.roommanager.ui.YMD
import com.lixh.roommanager.ui.YMD_HS
import com.lixh.roommanager.ui.bean.Room
import com.lixh.roommanager.ui.utils.UStore
import com.lixh.roommanager.ui.utils.toast
import kotlinx.android.synthetic.main.activity_edit.*


class EditActivity : AppCompatActivity() {
    var roomData by UStore(Constant.JSON_ROOM, "[]")
    lateinit var room: Room
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_edit)
        val actionBar: ActionBar? = supportActionBar
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true)
            actionBar.setDisplayHomeAsUpEnabled(true)
        }
        init()
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home -> {
                finish() // back button
                return true
            }
        }
        return super.onOptionsItemSelected(item)
    }

    private fun init() {
        room = intent.getSerializableExtra("Room") as Room
        if (room.startTime > 0 && room.endTime > 0) {
            tv_time_start.text = room.startTime.YMD_HS
            tv_time_end.text = room.endTime.YMD_HS
        }
        initState()
        tv_room_name.text = room.roomName
        tv_time_start.setOnClickListener {
            showTime(it as TextView, 1, "开始时间")
        }
        tv_time_end.setOnClickListener {
            showTime(it as TextView, 2, "结束时间")
        }
        //有人
        state_y.setOnClickListener {
            room.state = 2
            initState()
        }
        //空闲
        state_n.setOnClickListener {
            room.state = 1
            initState()
        }
    }

    private fun initState() {
        val state = room.state
        if (state == 2) {//有人
            state_y.setBackgroundColor(Color.parseColor("#ff314b"))
            state_y.setTextColor(Color.parseColor("#ffffff"))
            state_n.setBackgroundColor(Color.parseColor("#ffffff"))
            state_n.setTextColor(Color.parseColor("#333333"))
        } else {//空闲
            state_y.setBackgroundColor(Color.parseColor("#ffffff"))
            state_y.setTextColor(Color.parseColor("#333333"))
            state_n.setBackgroundColor(Color.parseColor("#ff314b"))
            state_n.setTextColor(Color.parseColor("#ffffff"))
        }
    }

    fun onSave(view: View) {
        //不是有人则还原为空闲状态
        if (room.state != 2) {
            room.startTime = 0
            room.endTime = 0
        }
        val rooms =
            Gson().fromJson<List<Room>>(roomData)
        val map = rooms.associateBy { it.roomName }.toMutableMap()
        map[room.roomName] = room
        roomData = Gson().toJson(map.values.toList())
        toast("保存成功")
    }

    private fun showTime(view: TextView, type: Int, desc: String) {
        val mDialogAll = TimePickerDialog.Builder()
            .setCallBack { _, millSeconds ->
                view.text = millSeconds.YMD
                //有人
                if (room.state == 2) {
                    //开始时间
                    if (type == 1) {
                        room.startTime = millSeconds
                    } else {
                        room.endTime = millSeconds
                    }
                }
            }
            .setCancelStringId("取消")
            .setSureStringId("确认")
            .setTitleStringId(desc)
            .setYearText("年")
            .setMonthText("月")
            .setDayText("日")
            .setHourText("时")
            .setMinuteText("分")
            .setCyclic(false)
            .setCurrentMillseconds(System.currentTimeMillis())
            .setThemeColor(resources.getColor(R.color.timepicker_dialog_bg))
            .setType(Type.YEAR_MONTH_DAY)
            .setWheelItemTextNormalColor(resources.getColor(R.color.timetimepicker_default_text_color))
            .setWheelItemTextSelectorColor(resources.getColor(R.color.timepicker_toolbar_bg))
            .setWheelItemTextSize(12)
            .build()
        mDialogAll.show(supportFragmentManager, "all")
    }
}